﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BhvFSM : MonoBehaviour
{
    [SerializeField]
    private List<string> stateList = new List<string>();

    [SerializeField]
    private bool debug = false;

    public string currentState { get => _currentState; }
    public string preState { get; }
    [SerializeField]private string _currentState = "";
    private string _preState = "";
    [HideInInspector]
    public float duration = 0;
    [HideInInspector]
    public bool paused = false;

    public void AddState(string state)
    {
        // state = state.ToLower();
        var _tempState = this.GetState(state);
        if (string.IsNullOrWhiteSpace(_tempState))
        {
            this.stateList.Add(state);

            if (this.stateList.Count == 1)
            {
                this._currentState = state;
            }
        }
        else
        {
            Debug.LogWarning($"FSM:Had added {state}");
        }
    }

    public void AddStates(string[] states)
    {
        foreach (var item in states)
        {
            this.AddState(item);
        }
    }

    public bool CompareState(string state)
    {
        return this._currentState == state;
    }

    public string GetState(string state)
    {
        // state = state.ToLower();
        if (this.stateList.IndexOf(state) != -1)
        {
            return state;
        }
        else
        {
            return null;
        }
    }

    public void ResetState()
    {
        this.ChangeState(this._currentState);
    }

    private string GetStringUpper(string s)
    {
        return s.Substring(0, 1).ToUpper() + s.Substring(1);
    }

    private void Update()
    {
        if (this.paused || Time.deltaTime == 0) return;

        if (String.IsNullOrEmpty(this._currentState) == false)
        {
            this.duration += Time.deltaTime;
            this.SendMessage($"On{GetStringUpper(this._currentState)}Update", null, SendMessageOptions.DontRequireReceiver);
        }
    }

    public void ChangeState(string state)
    {
        var _tmpState = this.GetState(state);
        if (_tmpState == null)
        {
            Debug.LogWarning($"FSM:STATE {state} NOT IN LIST");
        }

        if (string.IsNullOrWhiteSpace(this._currentState) == false)
        {
            this.SendMessage($"On{GetStringUpper(this._currentState)}Exit", null, SendMessageOptions.DontRequireReceiver);
        }
        this._preState = this._currentState;
        this._currentState = _tmpState;
        this.duration = 0;
        this.SendMessage($"On{GetStringUpper(this._currentState)}Enter", null, SendMessageOptions.DontRequireReceiver);
        if (this.debug)
        {
            print($"{this._preState} > {this._currentState}");
        }
    }



    public void RemoveAllState()
    {
        if (string.IsNullOrWhiteSpace(this._currentState) == false)
        {
            this.SendMessage($"On{GetStringUpper(this._currentState)}Exit", null, SendMessageOptions.DontRequireReceiver);
            this._currentState = null;
        }

        this.stateList.Clear();
    }

}
